//
//  BaseScrollViewController.swift
//  sss
//
//  Created by hrrMac on 2019/11/19.
//  Copyright © 2019 hrrMac. All rights reserved.
//

import UIKit
import MJRefresh
import KakaJSON

class BaseScrollViewController: BaseViewController {

    private var scrollView:UIScrollView?
    ///页数
    var page: Int = 0;
    ///是否刷新
    var isRefresh: Bool = true
    ///是否加载完成
    var isLoadingFinish: Bool = true
    
    var dataArr: [Convertible] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
}

///自动上下拉刷新逻辑
extension BaseScrollViewController{
    ///初始化上下拉刷新
    func initRefresh(_ scrollView: UIScrollView){
        initHeaderRefresh(scrollView)
        initFooterRefresh(scrollView)
    }
    
    ///初始化下拉刷新
    func initHeaderRefresh(_ scrollView :UIScrollView ){
        let header = MJRefreshNormalHeader {
            [weak self] in
            self?.headerRefreshAction()
        }
        header.isAutomaticallyChangeAlpha = true
        header.lastUpdatedTimeLabel?.isHidden = true
        scrollView.mj_header = header
        self.scrollView = scrollView
    }
    
    ///初始化上拉加载
    func initFooterRefresh(_ scrollView :UIScrollView ){
        let footer = MJRefreshBackNormalFooter {
            [weak self] in
            self?.footerRefreshAction()
        }
        scrollView.mj_footer = footer
        self.scrollView = scrollView
        scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 30, right: 0)
        scrollView.mj_footer?.ignoredScrollViewContentInsetBottom = 30
    }
    
    ///下拉刷新事件处理
    func headerRefreshAction(){
        isRefresh = true
        page = 0
        httpRequesterData()
    }
    
    ///上拉事件处理
    func footerRefreshAction(){
        if isLoadingFinish {
            scrollView?.mj_footer?.endRefreshing()
            return
        }
        httpRequesterData()
        
    }
    
    ///结束刷新动作
    func endRefresh(){
        scrollView?.mj_header?.endRefreshing()
        scrollView?.mj_footer?.endRefreshing()
    }
    
    ///下拉加载，带刷新动作
    func openHeaderRefresh(){
        scrollView?.mj_header?.beginRefreshing()
    }
    
    func httpRequesterData(){
        
    }
    
}


extension BaseScrollViewController{
    ///
    func httpListDataFinish(_ response: HttpRespons){
        endRefresh()
        if response.status == .success {
            
        }
        
        if isRefresh {
            dataArr.removeAll()
            isRefresh = false
        }
        
        
    }
}
